package com.fastmobile.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.json.JSONException;
import org.json.JSONObject;

import com.fastmobile.dao.TransactionsDAO;
import com.fastmobile.entity.Transactions;

public class TransactionsImpl implements TransactionsDAO{

	
	protected EntityManager em;
	
	public EntityManager getEntityManager() {
		return em;
	}
	
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}

	@Override
	public Transactions saveTransaction(Transactions transaction) {
		Transactions existTrans = null;
		if (null != transaction && null != transaction.getTransactionId()) {
			existTrans = getTransactionById(transaction.getTransactionId());
		}
		
		//update
		if (null != existTrans) {
			existTrans.setTransactionUnlock(transaction.isTransactionUnlock());
			existTrans.setDateofPrealert(transaction.getDateofPrealert());
			System.out.println("Today again: " + transaction.getDateofPrealert());
			em.getTransaction().begin();
//			dung khi them field 
//			em.merge(existTrans);
			existTrans.setTransactionUnlock(transaction.isTransactionUnlock());
			existTrans.setDateofPrealert(transaction.getDateofPrealert());
			em.getTransaction().commit();
		} else { // insert
			getEntityManager().persist(transaction);
		}
		
		return transaction;
	}

	@Override
	public void deleteTransactionById(String id) {
		Transactions trans = getTransactionById(id);
		if (null != trans) {
			em.remove(trans);
		}
	}
	
	@Override
	public void unlockTransactionById(String id, Date date){
		Transactions trans = getTransactionById(id);
		em.getTransaction().begin();
		trans.setTransactionUnlock(Boolean.TRUE);
		trans.setDateofPrealert(date);
		em.getTransaction().commit();
	}

	@Override
	public Transactions getTransactionById(String id) {
		return getEntityManager().find(Transactions.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Transactions> getAllTransaction() {
		Query query = em.createNativeQuery("SELECT p.TransID, p.TransUnlock, p.DateofPrealert FROM TRANSACTIONS p", Transactions.class);
		return query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Transactions> getTransactionByQuantity(int quantity) {
		String script = "SELECT TOP " + quantity + " p.TransID, p.TransUnlock, p.DateofPrealert FROM TRANSACTIONS p";
		Query query = em.createNativeQuery(script, Transactions.class);
//		query.setParameter("quantity", quantity);
		return query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Transactions> getLockedTransactionByQuantity(int quantity) {
		String script = "SELECT TOP " + quantity + " p.TransID, p.TransUnlock, p.DateofPrealert FROM TRANSACTIONS p WHERE p.TransUnlock = :transUnlock";
		Query query = em.createNativeQuery(script, Transactions.class);
		query.setParameter("transUnlock", Boolean.FALSE);
		return query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<JSONObject> getJsonLockedTransaction(int quantity) throws JSONException {
		List<Transactions> listTransaction = getLockedTransactionByQuantity(quantity);
		List<JSONObject> listObj = new ArrayList<JSONObject>();
		for (Transactions trans:listTransaction) {
			JSONObject json = new JSONObject();
			json.put("transId", trans.getTransactionId());
			json.put("transUnlock", trans.isTransactionUnlock());
			json.put("dateofPrealert", trans.getDateofPrealert());
			listObj.add(json);
		}
		return listObj;
	}
	
}
